﻿
$.ajaxSetup(
	{
		cache: true
	}
);

var KeyPair = function (title, value)
{
	this.value = ko.observableArray();

	this.and = function(title, value) {

		this.Value.push({Title: ko.observable(title), Value: ko.observable(value)});

		return this;
	};

	this.and(title, value);
};

var Element = function(typeName) {

	if (ViewModel.Metadata[typeName])

		return this;

	var result = ViewModel.Metadata[typeName] = function() { };

	result.TypeName = function () { return typeName; };

	result.CreateInstance = function () {

		var r = null;
		
		var model = ViewModel.Model();

		for (var e in model)
		{
			e = model[e];

			if (e.Metadata && e.Metadata() === this)

				return e;
		}
		
		r = new result();

		r.Metadata = function () {

			return result;
		};

		ViewModel.Model.push(r);

		return r;
	};

	result.Properties = ko.observableArray();

	this.property = function(name, defaultValue, title, values, editor)
	{
		result.prototype[name] = defaultValue;

		result.Properties.push(result[name] = { Name: name, Title: (!title) ? name : title, Value: values, Template: ko.observable("Design/" + ((editor) ? editor : "TextEditor")) });

		return this;
	};

	this.method = function (name, delegate)
	{
		result.prototype[name] = delegate;

		return this;
	};

	return this;
};

var animations = {

	Slide: function (element, index, data) {

        $(element).slideDown();
    }
};

var ViewModel = new (
	function ()
	{
		this.Metadata = {};

		this.Model = ko.observableArray();

		this.SelectedElement = ko.observable();
	}
)();

$(document)
.ready(
	function () {

		ko.applyBindings(ViewModel, $("html")[0]);
	}
);